package moni.anyou.com.view.view;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;
import org.kymjs.aframe.http.KJHttp;
import org.kymjs.aframe.http.KJStringParams;
import org.kymjs.aframe.http.StringCallBack;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import moni.anyou.com.view.R;
import moni.anyou.com.view.base.BaseFragment;
import moni.anyou.com.view.bean.request.ReqUpdateChildBaseInfoBean;
import moni.anyou.com.view.config.SysConfig;
import moni.anyou.com.view.tool.AppTools;
import moni.anyou.com.view.tool.ToastTools;
import moni.anyou.com.view.tool.Tools;
import moni.anyou.com.view.tool.UploadUtil;
import moni.anyou.com.view.view.fragmentadapter.DayAdapter;
import moni.anyou.com.view.view.my.MyTeamActivity;
import moni.anyou.com.view.view.my.PersonInfoSettingActivity;
import moni.anyou.com.view.view.my.SystemsNoticeActivity;
import moni.anyou.com.view.view.my.invitefamily.FamilyNumbersActivity;
import moni.anyou.com.view.view.my.systemset.SystemSettingActivity;
import moni.anyou.com.view.view.photo.PhotoDialog;
import moni.anyou.com.view.widget.NetProgressWindowDialog;
import moni.anyou.com.view.widget.dialog.MessgeDialog;
import moni.anyou.com.view.widget.pikerview.TimeSelector;
import moni.anyou.com.view.widget.pikerview.view.RelationSeletor;

public class FabuFragment extends BaseFragment implements View.OnClickListener {
    private View mView;
    private TimeSelector mStartTimeSelector;
    private TimeSelector mEndTimeSelector;
    ArrayList<String> mStringRelations = new ArrayList<>();
    ArrayList<String> mStringSexs = new ArrayList<>();
    private String mType;
    private String mVaule;
    private ImageView ivUpload;
    private String upLoadfile;
    private RelativeLayout rlUpload;
    private RelativeLayout rlStartTime;
    private RelativeLayout rlEndTime;
    private TextView tvStartTime;
    private TextView tvEndTime;
    private NetProgressWindowDialog window;
    public FabuFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_fabu, container, false);

        init(mView);


        return mView;
    }

    @Override
    public void initView() {
        super.initView();
        tvTitle = (TextView) mView.findViewById(R.id.page_title);
        tvTitle.setText("发布组团信息");
        window = new NetProgressWindowDialog(mBaseActivity);

        rlUpload = (RelativeLayout)mView.findViewById(R.id.rl_upload);
        ivUpload = (ImageView) mView.findViewById(R.id.iv_upload);
        rlStartTime = (RelativeLayout)mView.findViewById(R.id.rl_startTime);
        rlEndTime = (RelativeLayout)mView.findViewById(R.id.rl_endTime);
        tvStartTime = (TextView) mView.findViewById(R.id.tv_startTime);
        tvEndTime = (TextView)mView.findViewById(R.id.tv_endTime);
    }


    @Override
    public void onResume() {
        super.onResume();

    }
    @Override
    public void setAction() {
        super.setAction();
        rlUpload.setOnClickListener(this);
        rlStartTime.setOnClickListener(this);
        rlEndTime.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_upload:
                mPhotoDialog.show();
                break;
            case R.id.rl_startTime:
                mStartTimeSelector.show();
                break;
            case R.id.rl_endTime:
                mEndTimeSelector.show();
                break;
            default:
                break;
        }
    }
    private void initSeleterData() {



        mStartTimeSelector = new TimeSelector(mContext, new TimeSelector.ResultHandler() {
            @Override
            public void handle(String time) {
                if (time != null) {

                   tvStartTime.setText(time);

                }

            }
        }, Tools.getYeartoMiniteNowtime(), Tools.getYeartoMiniteNowtime());
        mEndTimeSelector = new TimeSelector(mContext, new TimeSelector.ResultHandler() {
            @Override
            public void handle(String time) {
                if (time != null) {
                    tvEndTime.setText(time);
                }

            }
        }, Tools.getYeartoMiniteNowtime(), Tools.getYeartoMiniteNowtime());

    }
    public void addTeamInfo() {
        window.ShowWindow();


         //上传图片
            if (upLoadfile.contains("storage")) {

                UploadUtil.ossUpload(mContext,new File(upLoadfile),"team",upLoadfile);

            } else {

                UploadUtil.ossUpload(getActivity(),new File(getActivity().getApplication().getCacheDir() +  "/qianmoFile/" + upLoadfile),"headicon",getActivity().getApplication().getCacheDir() +  "/qianmoFile/" + upLoadfile);

            }

        KJHttp kjh = new KJHttp();
        KJStringParams params = new KJStringParams();
        String cmdPara = new ReqUpdateChildBaseInfoBean("6", SysConfig.uid, SysConfig.token, mType, mVaule).ToJsonString();
        params.put("sendMsg", cmdPara);

        kjh.urlGet(SysConfig.ServerUrl, params, new StringCallBack() {
            @Override
            public void onSuccess(String t) {

                // Log.d(TAG, "onSuccess: " + t);
                try {
                    JSONObject jsonObject = new JSONObject(t);
                    int result = Integer.parseInt(jsonObject.getString("result"));
                    if (result >= 1) {

                        Toast.makeText(mContext, "修改成功", Toast.LENGTH_LONG).show();
                    } else {
                        if (result == -2) {
                            AppTools.jumptoLogin(mBaseActivity, result);
                        }
                        else {
                            Toast.makeText(mContext, jsonObject.get("retmsg").toString(), Toast.LENGTH_LONG).show();
                        }
                    }
                } catch (Exception ex) {
                    Toast.makeText(mContext, "数据请求失败", Toast.LENGTH_LONG).show();

                }
                window.closeWindow();
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Toast.makeText(mContext, "网络异常，请稍后再试", Toast.LENGTH_LONG).show();

                window.closeWindow();
            }
        });
    }

    private String getFileName(String dir){
        //取得最后一个/的下标
        dir = dir.replace("\\","/");
        int index = dir.lastIndexOf("/");
        String newString = dir.substring(index + 1);
        return newString;
    }



    private PhotoDialog mPhotoDialog = null;

    private PhotoDialog.PhotoListener mPhotoListener = new PhotoDialog.PhotoListener() {
        @Override
        public void onSuccess(List<String> photoList) {

            if (null != photoList) {
                final File file = new File(photoList.get(0));
                /*
                if (file.exists()) {
                    mVaule = file.getName();
                    postUpdateInfo();
                    upLoadfile = file;
                    Uri imageUri = Uri.fromFile(file);
                    UploadThread m = new UploadThread();
                    new Thread(m).start();

                } else {
                    ToastTools.showShort(mContext, "头像文件不存在");
                }*/
                if (file.exists()) {

                    //Toast.makeText(mContext, photoList.get(0), Toast.LENGTH_LONG).show();
                    setBitmaptoImageView("file://" + photoList.get(0),  ivUpload, R.drawable.loading_11,
                            R.drawable.loading_err_11, R.drawable.loading_null_11);
                    upLoadfile = AppTools.comparese(mContext, photoList.get(0));
                    //Toast.makeText(mContext,  upLoadfile , Toast.LENGTH_LONG).show();
                    if (upLoadfile.contains("storage")) {
                        mVaule = "team/"+ getFileName(upLoadfile);
                    }
                    else {
                        mVaule = "team/"+upLoadfile;
                    }

                    //postUpdateInfo();
                    //ToastTools.showShort(mContext, filename);
                  /*
                   if (filename.contains(SysConfig.File_DIR)) {
                       map.put(mType,"ads/"+filename);
                       ToastTools.showShort(mContext, getApplication().getCacheDir() + "/qianmoFile/" + filename);

                       upLoadfile = new File (getApplication().getCacheDir() + "/qianmoFile/" + filename);
                       if (upLoadfile.exists()){
                           ToastTools.showShort(mContext, "头像文件存在");
                       }
                       else{
                           ToastTools.showShort(mContext, "头像文件不存在");
                       }

                   } else {
                        map.put(mType,"ads/"+filename);
                        upLoadfile = file;
                   }
                   */
                    // Uri imageUri = Uri.fromFile(file);


                } else {
                    ToastTools.showShort(mContext, "文件不存在");
                }

            }
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError() {

        }

        @Override
        public void onFinish() {

        }
    };





}
